
%{
#include 	<string.h>
void comentario (void);
char tratachar (char *);
%} 

delim		[ \t\n\r]
ws			{delim}+
digito  	[0-9]
letra		[A-Za-z]
intct		{digito}+
carac1  	\\.|[^\\']
charct  	'{carac1}'
floatct     {digito}+\.{digito}*([Ee][+-]?{digito}+)?
carac2		\\.|[^\\\"]
string		\"{carac2}*\"
id			{letra}({letra}|{digito})*

%%

{ws}		{ ;}
"/*"		{comentario ();}
char		{return CHAR;}
default		{return DEFAULT;}
do   		{return DO;}
else		{return ELSE;}
false		{return FALSE;}
float		{return FLOAT;}
for 		{return FOR;}
if 	    	{return IF;}
int   		{return INT;}
logic		{return LOGIC;}
read		{return READ;}
repeat		{return REPEAT;}
statements	{return STATEMENTS;}
switch		{return SWITCH;}
then		{return THEN;}
to  		{return TO;}
true		{return TRUE;}
until		{return UNTIL;}
variables	{return VARIABLES;}
while		{return WHILE;}
write		{return WRITE;}
{id}		{strcpy (yylval.string, yytext); return ID;}
{charct}  	{yylval.carac = yytext[3] == '\0' ? yytext[1] : yytext[2]; return CHARCT;}
{intct}  	{yylval.valint = atoi(yytext); return INTCT;}
{floatct}  	{yylval.valreal = atof(yytext); return FLOATCT;}
{string}	{strcpy (yylval.string, yytext); return STRING;}
"||"       	{return OROP;}
"&&"       	{return ANDOP;}
"!"       	{return NOTOP;}
"<"       	{yylval.atr = LT; return RELOP;}
"<="		{yylval.atr = LE; return RELOP;}
">" 		{yylval.atr = GT; return RELOP;}
">="		{yylval.atr = GE; return RELOP;}
"=="		{yylval.atr = EQ; return RELOP;}
"!="		{yylval.atr = NE; return RELOP;}
"+"       	{yylval.atr = MAIS; return ADOP;}
"-"	       	{yylval.atr = MENOS; return ADOP;}
"*" 		{yylval.atr = MULT; return MULTOP;}
"/" 		{yylval.atr = DIV; return MULTOP;}
"%" 		{yylval.atr = RESTO; return MULTOP; printf("*found a %*");}
"~"       	{return NEGOP;}
"("       	{return OPPAR;}
")"       	{return CLPAR;}
"["       	{return OPBRAK;}
"]"       	{return CLBRAK;}
"{"       	{return OPBRACE;}
"}"       	{return CLBRACE;}
";"       	{return SCOLON;}
","       	{return COMMA;}
":"       	{return COLON;}
"="       	{return ASSIGN;}
.    		{yylval.carac = yytext[0]; return INVAL;}

%%

void comentario () {
	char c;  int estado;
	estado = 1;
	while (estado != 3) {
		switch (estado) {
			case 1:
				c = input ();
				if (c == EOF) estado = 3;
				else if (c == '*') estado = 2; break;
			case 2:
				c = input ();
				if (c == EOF || c == '/') estado = 3;
				else if (c != '*') estado = 1; break;
		}
	}
}

/*
	tratachar: retorna o codigo ASCII de uma constante do tipo char, eliminando os apostrofos (') e as barras invertidas (\)
 */

char tratachar (char *s) {
	if (s[1] != '\\') return s[1];
	else switch (s[2]) {
		case 'a': 	return 7;		case '\\': 	return 92;
		case 'b': 	return 8;		case 'r': 	return 13;
		case '\"': 	return 34;		case 'f': 	return 12;
		case 't': 	return 9;		case 'n': 	return 10;
		case '0': 	return 0;		case '\'': 	return 39;
		case 'v': 	return 11;
		default:	return s[2];
	}
}
